if (SERVER) then 
   AddCSLuaFile ("shared.lua") 
 
   SWEP.Weight = 5
   SWEP.AutoSwitchTo = false
   SWEP.AutoSwitchFrom = false
 
end
 
if (CLIENT) then --the cl_init.lua stuff goes in here
 
 
   SWEP.PrintName = "Prop Thrower"
   SWEP.Slot = 1
   SWEP.SlotPos = 1
   SWEP.DrawAmmo = false
   SWEP.DrawCrosshair = false
 
end
 
 
SWEP.Author = ""
SWEP.Contact = ""
SWEP.Purpose = "Assists in throwing props"
SWEP.Instructions = "Left click to shoot a prop"
SWEP.Category = "Prop Launchers"
 
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
 
SWEP.ViewModel = "models/weapons/v_pistol.mdl"
SWEP.WorldModel = "models/weapons/w_pistol.mdl"
 
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Primary.Recoil = 4

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"

local ShootSound = Sound("weapons/airboat/airboat_gun_energy2.wav")
 

function SWEP:Reload()
end
 
function SWEP:Think()
end

function SWEP:throw_attack (model_file)
	if model_file == nil or model_file == "" then 
		model_file = "models/Combine_Helicopter/helicopter_bomb01.mdl"
	end
	local tr = self.Owner:GetEyeTrace()
 
	if (!SERVER) then return end

	local ent = ents.Create ("prop_physics")
	if !ent then return end
	if !ent:IsValid() then return end
	
	
	ent:SetModel (model_file)	
	ent:SetPos (self.Owner:EyePos() + (self.Owner:GetAimVector() * 16))
	ent:SetAngles (self.Owner:EyeAngles())
	ent:Spawn()
	ent:SetOwner(self.Owner)
	ent:SetColor(team.GetColor(self.Owner:Team()))
	ent:SetMaterial("models/debug/debugwhite")
	local trail = util.SpriteTrail(ent, 0, team.GetColor(self.Owner:Team()), false, 15, 1, 4, 1/(15+1)*0.5, "trails/laser.vmt")
	local ply = self.Owner
	if ply.props == nil then ply.props = {} end

	table.insert(ply.props, ent)
		
	local index = ent.propIndex
	local maxProps = GetConVar("pf_maxprops"):GetInt()
	if(#ply.props > maxProps) then
		ply.props[1]:Remove()
		table.remove(ply.props, 1)
	end
	
	local phys = ent:GetPhysicsObject()
	if !phys:IsValid() then return end
	phys:ApplyForceCenter(ply:GetAimVector() * phys:GetMass() * ply.propData.propForce)
	
	--local rnda = self.Primary.Recoil * -1 
	--local rndb = self.Primary.Recoil * math.random(-1, 1) 	
	--self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) 
	
end

function SWEP:PrimaryAttack()
	local ply = self.Owner	

	if CLIENT then
		if ply:GetNWBool("canfire") then
			local ShootSound = Sound("weapons/airboat/airboat_gun_energy2.wav")
			self.Weapon:EmitSound(ShootSound) 
			self.BaseClass.ShootEffects(self)
		end
	end
	if SERVER then 
		if ply.lastfire == nil then ply.lastfire = 0 end 
		local time = CurTime() - ply.lastfire 
		
		if time >= ply.propData.propRate then 
			ply:SetNWBool("canfire", false)
			umsg.Start("fire_delay")
				umsg.Short(math.floor(ply.propData.propRate))
			umsg.End()
			ply.lastfire = CurTime() 
			
			timer.Simple(ply.propData.propRate, function()
				ply:SetNWBool("canfire", true)
			end)
			
			self:throw_attack ( ply.propData.prop ) 
		end
	end	

end
function SWEP:SecondaryAttack()

	local tr = self.Owner:GetEyeTrace()
	if !tr.Entity then return end
	if !tr.Entity:IsValid() or tr.Entity:IsWorld() then return end

	local shot_length = tr.HitPos:Length()
	local phys = tr.Entity:GetPhysicsObject()
	if !phys:IsValid() then return end
	phys:ApplyForceCenter(self.Owner:GetAimVector():GetNormalized() *  math.pow(shot_length, 10))

end


